package com.situ.ssm.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.ssm.common.ServerResponse;
import com.situ.ssm.dao.StudentMapper;
import com.situ.ssm.entity.Student;
import com.situ.ssm.service.IStudentService;

@Service
public class StudentServiceImpl implements IStudentService{
	
	@Autowired
	private StudentMapper studentMapper;

	@Override
	public ServerResponse pageList(Integer page, Integer limit, Student student) {
		// 1、使用PageHelper插件设置分页
		PageHelper.startPage(page, limit);
		// 2、查询当前页的所有数据
		List<Student> list = studentMapper.pageList(student);
		//用PageInfo对结果进行包装
		PageInfo pageInfo = new PageInfo(list);
		Integer count = (int) pageInfo.getTotal();
		
		return ServerResponse.createSuccess("", count, list);
	}

	@Override
	public ServerResponse deleteById(Integer id) {
		int count = studentMapper.deleteById(id);
		if (count != 1) {
			return ServerResponse.createError("删除失败");
		}
		
		return ServerResponse.createSuccess("删除成功");
	}

	@Override
	public ServerResponse deleteAll(Integer[] ids) {
		int count = studentMapper.deleteAll(ids);
		if (count != ids.length) {
			return ServerResponse.createError("删除失败");
		}
		
		return ServerResponse.createSuccess("删除成功");
	}
	@Override
	public ServerResponse insert(Student student) {
		int count = studentMapper.insert(student);
		if (count != 1) {
			return ServerResponse.createError("添加失败");
		}
		
		return ServerResponse.createSuccess("添加成功");
	}

	@Override
	public Student selectById(Integer id) {
		// TODO Auto-generated method stub
		return studentMapper.selectById(id);
	}
	@Override
	public boolean update(Student student) {
		//SqlSession sqlSession = MyBatisUtil.getSqlSession();
		//studentMapper = sqlSession.getMapper(IStudentMapper.class);
		int count = studentMapper.update(student);
	//	sqlSession.commit();
	//	sqlSession.close();
		return count == 1 ? true : false;
		//return true;
	}
	

}
